import {contextBridge, ipcRenderer} from 'electron'
import {electronAPI} from '@electron-toolkit/preload'

import {
    api_current_summoner_info,
    api_current_summoner_profile_info, api_get_summoner_info_by_puuid, api_search_summoner_by_name
} from "@/main/apis/summoner.js";
import {api_current_ranked_list, api_get_ranked_list_by_puuid} from "@/main/apis/rank.js";
import store, {store_field_lcu_certificate} from "@/main/store/store.js";
import {invoke_url} from "@/main/apis/dev/dev_api.js";
import {api_search_game_detail_by_game_id, api_search_history_by_puuid} from "@/main/apis/gamerecord.js";
import {
    api_get_augment_json, api_get_champion_detail,
    api_get_champion_summoner,
    api_get_item_json, api_get_map, api_get_map_asserts, api_get_perk_json,
    api_get_perkstyle,
    api_get_queue_json, api_get_spell
} from "@/main/apis/baseData.js";
import {
    api_get_champ_select_session,
    api_get_game_flow_session,
    api_get_gameflow_phase,
    api_get_lobby_session
} from "@/main/apis/game-flow.js";
import {api_get_summoner_profile_by_puuid} from "@/main/apis/summoner.js";


const api = {
    "summoner": {
        "currentSummoner": () => {
            return api_current_summoner_info();
        },
        "currentSummonerProfile": () => {
            return api_current_summoner_profile_info();
        },
        'getSummonerProfileByPuuid':(puuid)=>{
            return api_get_summoner_profile_by_puuid(puuid);
        },
        "getSummonerInfoByPuuid":(puuid)=>{
            return  api_get_summoner_info_by_puuid(puuid);
        },
        "getSummonerInfoByName":(data)=>{
            return api_search_summoner_by_name(data);
        }
    },
    "gameflow": {
        "currentPhase":()=>{
            return api_get_gameflow_phase();
        },
        "championSelectSession":()=>{
            return api_get_champ_select_session();
        },
        "getLobbySession":()=>{
            return api_get_lobby_session();
        },
        "getGameFlowSession":()=>{
            return api_get_game_flow_session();
        }
    },
    "rank": {
        "getRankedListByPuuid": (puuid) => {
            return api_get_ranked_list_by_puuid(puuid)
        },
        "currentRankedList": () => {
            return api_current_ranked_list();
        }
    },
    "gamereocrd": {
        "searchByPuuid": (param) => {
            return api_search_history_by_puuid(param);
        },
        "searchGameDetailByGameId": (param) => {
            return api_search_game_detail_by_game_id(param);
        }
    }
    ,
    "baseData": {
        "queue_json": () => {
            return api_get_queue_json()
        },
        "item_json": () => {
            return api_get_item_json()
        },
        "perkstyle_json": () => {
            return api_get_perkstyle()
        },
        "perks_json":()=>{
            return api_get_perk_json();
        },
        "champion_summoner": () => {
            return api_get_champion_summoner();
        },
        "champion_detail":(champion_id) => {
            return api_get_champion_detail(champion_id);
        },
        "augment": () => {
            return api_get_augment_json();
        },
        "map_json": () => {
            return api_get_map();
        },
        "map_asserts": () => {
            return api_get_map_asserts();
        },
        "spell_json": () => {
            return api_get_spell();
        }
    }

}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
    try {
        contextBridge.exposeInMainWorld('electron', electronAPI)

        //lcuApi 实时调用英雄联盟接口
        contextBridge.exposeInMainWorld('api', api)
        //获取环境信息
        contextBridge.exposeInMainWorld('env', {
            "getEnv": (key) => {
                return ipcRenderer.invoke('evn/getEnv', key);
            }
        })

        //开发工具暴露的api
        contextBridge.exposeInMainWorld('api_developer', {
            "getCertificate": () => {
                return store.get(`${store_field_lcu_certificate}`);
            },
            "api_invoke": (param) => {
                return invoke_url(param);
            }
        })
    } catch (error) {
        console.error(error)
    }
} else {
    window.electron = electronAPI
    window.api = api
}
